package com.hdjd.controller;

import com.hdjd.entities.Account;
import com.hdjd.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 刘子强
 * 2021/2/5 16:48
 */
@Controller
@RequestMapping("/register")
public class RegisterController {
    @Autowired
    private AccountService accountService;

    //用户注册
    @ResponseBody
    @RequestMapping("/accountRegister")
    public Map<String, Object> accountRegister(Account account){
        Map<String, Object> map = new HashMap<>();

        boolean flag;
        try {
            flag = this.accountService.insertAccount(account);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "注册失败");
            return map;
        }

        if(flag){
            map.put("code", 1001);
            map.put("msg", "注册成功");
        }else{
            map.put("code", 1002);
            map.put("msg", "注册失败");
        }
        return map;
    }

    //校验用户名是否可用
    @ResponseBody
    @RequestMapping("/accountNameExist")
    public Map<String, Object> accountNameExist(String accName){
        Map<String, Object> map = new HashMap<>();

        boolean flag;
        try {
            flag = this.accountService.selectAccountByAccName(accName);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "该用户名已存在");
            return map;
        }

        if(flag){
            map.put("code", 1002);
            map.put("msg", "该用户名已存在");
        }else{
            map.put("code", 1001);
            map.put("msg", "用户名可用");
        }
        return map;
    }
}
